# -*- coding:utf-8 -*-

# @Time    : 18-11-12 下午10:30

# @Author  : Swing


import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, [None, 2], name='x')
y = tf.placeholder(tf.float32, [None, 1], name='y')

w = tf.Variable(tf.zeros([2, 1]), name='w')
b = tf.Variable(tf.zeros([1]), name='b')

logits = tf.matmul(x, w) + b
output = tf.nn.sigmoid(logits)
cross_entropy = tf.losses.sigmoid_cross_entropy(multi_class_labels=y, logits=logits)

train_step = tf.train.GradientDescentOptimizer(0.3).minimize(cross_entropy)

print(train_step)

x_values = np.array(
    [
        [1, 1],
        [1, 0],
        [0, 1],
        [0, 0]
    ]
)

y_values = np.array(
    [
        [1],
        [1],
        [1],
        [0]
    ]
)

init_op = tf.global_variables_initializer()

sess = tf.Session()

sess.run(init_op)

cross_entropy_value, logits_value, output_value = sess.run(
    [cross_entropy, logits, output],
    feed_dict={x: x_values,
               y: y_values}
)

print(cross_entropy_value)
print(logits_value)
print(output_value)

for current_step in range(100):
    cross_entropy_value, output_value, _ = sess.run(
        [cross_entropy, output, train_step],
        feed_dict={x: x_values, y: y_values}
    )

    # print(train_step_value)
    print(cross_entropy_value)
    print(logits_value)
    print(output_value)
    print('-----------------------------------------------------')


cross_entropy_value, logits_valuem, output_value, w_value, b_value = sess.run(
    [cross_entropy, logits, output, w, b],
    feed_dict={x: x_values, y: y_values}
)


pass
